﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using umbraco.cms.businesslogic.member;

namespace osMemberControls
{
    public partial class MemberForgotUsername : System.Web.UI.UserControl
    {
        private string _userName, _subject;
        private int _redirectID;


        public string EmailLabel
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public string Subject
        {
            get { return _subject; }
            set { _subject = value; }
        }



        public int RedirectID
        {
            get { return _redirectID; }
            set { _redirectID = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblEmail.Text = EmailLabel;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                MailMessage mm = new MailMessage();
                SmtpClient s = new SmtpClient();
                Member m = Member.GetMemberFromEmail(txtUserName.Text);
                             
               
                mm.To.Add(m.Email);
                mm.Subject = Subject;
                mm.Body = "Your username is " + m.LoginName.ToString();
                mm.Body = mm.Body.Replace("##loginname##", m.LoginName);
                mm.Body = mm.Body.Replace("##email##", m.Email);
                mm.IsBodyHtml = true;

                s.Send(mm);

                Response.Redirect(umbraco.library.NiceUrl(RedirectID));
            }
        }
                
    }
}